/*
 * @Author: xiehangyun 
 * @Date: 2025-02-24 13:28:52 
 * @Last Modified by: xiehangyun
 * @Last Modified time: 2025-02-27 19:01:51
 */


import addMockupBgService from "@/api/services/addMockupBgService";
import utilsServer from "@/api/services/utilsServer";
import utils from "@/utils/utils";
import { debounce, isEmpty } from "lodash";
import { observable, configure } from "mobx";
configure({ enforceActions: "never" });

const addBgModalStore = observable({

  // 是否显示添加背景的弹窗
  open: false,
  setOpen(open: boolean) {
    this.open = open;
  },

  // 上身效果列表
  mockuplist: [] as Array<string>,
  //上身效果背景fabric数据
  mockupDatas: [] as Array<{}>,
  setMockuplist(mockuplist: Array<string>, mockupDatas: Array<{}> = mockuplist.map(() => ({}))) {
    this.mockuplist = mockuplist;
    this.mockupDatas = mockupDatas;
    this.setOpen(!isEmpty(mockuplist));
    if (!isEmpty(mockuplist)) {
      this.setCurrentMockup(mockuplist[0], 0);
    }
  },

  //修改上身效果背景数据
  changeMockupData: debounce((index: number, mockupData: any) => {
    addBgModalStore.mockupDatas[index] = mockupData;
  }, 100),

  //当前选中的效果图
  currentMockup: "",
  currentIndex: 0,
  setCurrentMockup(currentMockup: string, currentIndex: number) {
    this.currentMockup = currentMockup;
    this.currentIndex = currentIndex;
  },

  // 背景图列表
  uploadbglist: [] as Array<string>,
  setUploadbglist(uploadbglist: Array<string>) {
    this.uploadbglist = uploadbglist;
  },

  // 当前背景图列表
  bglist: [] as Array<{ id: number, picPath: string }>,
  setBglist(bglist: Array<{ id: number, picPath: string }>) {
    this.bglist = bglist;
  },
  //背景图类型 1自己上传 2官方
  bgtype: 2,
  setBgtype(bgtype: number) {
    this.bgtype = bgtype;
    this.setCurrent(1);
  },

  //当前分页
  current: 1,
  //每页数量
  pageSize: 8,
  //总数量
  total: 0,
  setCurrent(current: number) {
    this.current = current;
    addMockupBgService.getBgImgList(current, this.pageSize, this.bgtype).then((data) => {
      if (isEmpty(data.list) && current > 1) {
        this.setCurrent(current - 1);
      } else {
        this.setBglist(data.list);
        this.total = data.totalCount;
      }
    });
  },

  // 当前选中的背景图
  currentBg: "" as string | `#${string}`,
  setCurrentBg(currentBg: string) {
    this.currentBg = currentBg;
  },

  //背景的fabric数据数组
  bgDatas: [] as Array<{}>,
  setBgDatas(bgDatas: Array<{}>) {
    this.bgDatas = bgDatas;
  },

  //上传背景图
  async uploadBg() {
    const file = await utils.selectFile();
    if (file) {
      const url = await utilsServer.uploadOss(file);
      addMockupBgService.addBgImg(url).then((data) => {
        this.setCurrent(1);
      });
    }
  },

  //通过id删除背景图
  async deleteBg(id: number) {
    addMockupBgService.delBgImg(id).then((data) => {
      this.setCurrent(this.current);
    });
  },

  //保存
  save() {
    const { currentBg } = this;
  }

}, {}, { deep: false });

export default addBgModalStore;